package cn.minimelon.solon.service.warehouse.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.minimelon.solon.domain.warehouse.DeptBuyDetailVO;
import cn.minimelon.solon.domain.warehouse.PickDetailVO;
import cn.minimelon.solon.mapper.warehouse.DeptBuyDetailMapper;
import cn.minimelon.solon.mapper.warehouse.PickDetailMapper;
import cn.minimelon.solon.service.warehouse.DeptBuyDetailService;
import cn.minimelon.solon.utils.UnitUtil;
import org.beetl.sql.solon.annotation.Db;
import org.noear.solon.annotation.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DeptBuyDetailServiceImpl implements DeptBuyDetailService {
    @Db("writer")
    private PickDetailMapper pickDetailMapper;

    @Db("writer")
    private DeptBuyDetailMapper deptBuyDetailMapper;

    @Override
    public List<DeptBuyDetailVO> selectListWithCalcQty(List<String> idList, String logicId, Integer genFlag) {
        List<DeptBuyDetailVO> subList = deptBuyDetailMapper.selectListByPid(idList, logicId, genFlag);
        if (CollUtil.isEmpty(subList)) {
            return subList;
        }
        calcDetailWithQty(logicId, subList, idList, genFlag);
        return subList;
    }

    @Override
    public int selectUnGenFinishCount(DeptBuyDetailVO query) {
        return 0;
    }

    private void calcDetailWithQty(String logicId, List<DeptBuyDetailVO> subList, List<String> idList, int genFlag) {
        // 查询单据对应的拣货单明细信息
        List<PickDetailVO> detailList = pickDetailMapper.selectAllFromSourceBill(idList, logicId);
        Map<String, List<PickDetailVO>> detailGroup = detailList.stream()
                .collect(Collectors.groupingBy(PickDetailVO::getSourceId));
        for (DeptBuyDetailVO sub : subList) {
            sub.setStockQty(BigDecimal.ZERO);
            sub.setOccupyQty(BigDecimal.ZERO);
            sub.setLockQty(BigDecimal.ZERO);

            List<PickDetailVO> pickedList = detailGroup.get(sub.getId());
            if (CollUtil.isEmpty(pickedList)) {
                sub.setSendQty(BigDecimal.ZERO);
                sub.setSendPkgQty(BigDecimal.ZERO);
                sub.setPickedQty(BigDecimal.ZERO);
                sub.setWaitQty(BigDecimal.ZERO);
                sub.setTaskQty(sub.getApplyQty());
            } else {
                // 待检货数量 = 已生成拣货单，且未完成和未关闭
                BigDecimal pickedQty = pickedList.stream()
                        .filter(o -> o.getPickStatus() != 50 && o.getPickStatus() != 40)
                        .map(item -> item.getQty().multiply(item.getPkgDefQty()))
                        .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
                // 待检货数量
                sub.setPickedQty(pickedQty);
                // 已发数量 = 院内验收驳回的情况，只能取单据明细的已发数量
                BigDecimal sendQty = sub.getSendQty();
                sub.setSendQty(sendQty);
                sub.setSendPkgQty(sendQty.divide(pickedList.get(0).getPkgDefQty(), RoundingMode.UNNECESSARY));
                // 拣货中，就是待发
                sub.setWaitQty(pickedQty);
                // 待生成任务数 = 请领 - 已发 - 待拣货
                sub.setTaskQty(sub.getApplyQty().subtract(sendQty).subtract(pickedQty));
            }
        }
    }
}
